<?php

/**
 * @author Invisible Man
 * @copyright 2013
 */
require ('../lib/admin_auth.php');
require ('../config.php');
require ('../lib/db_connect.php');
//Xử lý dữ liệu
$query = "SELECT SUM(order_price) FROM order_item";
$result = mysql_query($query, $con);
$data = mysql_fetch_assoc($result);
$doanhthu = $data['SUM(order_price)'];
if (isset($_GET['name'])) {
    $name = $_GET['name'];
}
/**
 ****************************************************************************
 * STATS PAGE
 *************************************************************************** */
if ($name == "Stats") {
    echo '
    <table class="stats">
    <tr>
        <td class="result title">Doanh thu</td>
        <td class="result">' . number_format($doanhthu) . ' VNĐ</td>  
    </tr>';
    $query = "SELECT * FROM order_item as o, item as i WHERE o.item_name=i.item_name";
    $result = mysql_query($query, $con);
    $giagoc = 0;
    while ($data = mysql_fetch_assoc($result)) {
        $giagoc += $data['item_price'] * $data['order_quantity'];
    }
    $lai = $doanhthu - $giagoc;
    echo '
    <tr>
        <td class="result title">Lãi thật</td>
        <td class="result">' . number_format($lai) . ' VNĐ</td>
    </tr>';

    $query = "SELECT * FROM item";
    $result = mysql_query($query, $con);
    $vondautu = 0;
    while ($data = mysql_fetch_assoc($result)) {
        $vondautu += $data['item_price'] * $data['item_quantity'];
    }

    echo '
    <tr>
        <td class="result title">Vốn đầu tư</td>
        <td class="result">' . number_format($vondautu) . ' VNĐ</td>
    </tr>';
    $query = "SELECT item_name, SUM(order_quantity) FROM order_item GROUP BY item_name ORDER BY order_quantity DESC";
    $result = mysql_query($query, $con);
    $data = mysql_fetch_assoc($result);
    $highest = $data['item_name'];
    echo '
    <tr>
        <td class="result title">Mua nhiều nhất</td>
        <td class="result">' . $highest . '</td>
    </tr>';
    $query = "SELECT item_name, SUM(order_quantity) FROM order_item GROUP BY item_name ORDER BY order_quantity ASC";
    $result = mysql_query($query, $con);
    $data = mysql_fetch_assoc($result);
    $lowest = $data['item_name'];
    echo '
    <tr>
        <td class="result title">Mua ít nhất</td>
        <td class="result">' . $lowest . '</td>
    </tr>
</table>';
} elseif ($name == "Order List") {
    //////////////////////ORDER LIST ///////////////////////

?> 
    <table class="data_list">
    <tr>
        <th class="list_text list_title">STT</th>
        <th class="list_text list_title">Tên hóa đơn</th>
        <th class="list_text list_title">ID</th>
        <th class="list_text list_title">Tên hàng hóa</th>
        <th class="list_text list_title">Loại hàng</th>
        <th class="list_text list_title">Xuất xứ</th>
        <th class="list_text list_title">Số lượng</th>
        <th class="list_text list_title">Tình trạng</th>
        <th class="list_text list_title">Ngày xuất</th>
        <th class="list_text list_title"style="width: 80px;">Quản lý</th>
    </tr>
    <?php
    //Ghi dữ liệu
    require ('../lib/db_connect.php');
    $query = 'SELECT * FROM order_item as o, item_cate as c, item_country as n, item as i WHERE (o.cate_name=c.cate_name AND o.country_name=n.country_name) AND o.item_name=i.item_name ORDER BY order_id DESC, order_status ASC';
    $result = mysql_query($query, $con);
    $count = 1;
    while ($data = mysql_fetch_assoc($result)) {
        echo '
            <tr>
                <td class="list_text">' . $count . '</td>
                <td class="list_text">' . $data['order_buyer'] . '</td>
                <td class="list_text">' . $data['item_id'] . '</td>
                <td class="list_text">' . $data['item_name'] . '</td>
                <td class="list_text">' . $data['cate_name'] . '</td>
                <td class="list_text">' . $data['country_name'] . '</td>
                <td class="list_text">' . $data['order_quantity'] . '</td>
                <td class="list_text">';
        switch ($data['order_status']) {
            case 1:
                echo '<strong style="color:green;">Đã nhận</strong>';
                break;
            case 2:
                echo '<strong style="color:red;">Đang xử lý</strong>';
                break;
        }
        echo '</td>
                <td class="list_text">' . date("d/m/Y", $data['order_date_out']) .
            '</td>
                <td class="list_text"><a href="order_edit.php?id=' . $data['order_id'] .
            '" class="btnEdit"></a><a href="order_del.php?id=' . $data['order_id'] .
            '" class="btnDel" onclick="return user_confirm(\'Bạn có muốn xóa hàng hóa này không?\');"></a></td>
            </tr>';
        $count++;
    }
?>
</table>
<?php
} elseif ($name == "Item List") {
    /**
     * ***********************************************************************
     * ITEM LIST
     * ***********************************************************************/
?>
<table class="data_list">
    <tr>
        <th class="list_text list_title">STT</th>
        <th class="list_text list_title">Tên hàng hóa</th>
        <th class="list_text list_title">Loại hàng</th>
        <th class="list_text list_title">Xuất xứ</th>
        <th class="list_text list_title">Số lượng</th>
        <th class="list_text list_title">Tình trạng</th>
        <th class="list_text list_title">Ngày nhập</th>
        <th class="list_text list_title"style="width: 80px;">Quản lý</th>
    </tr>
    <?php
    //Ghi dữ liệu
    require ('../lib/db_connect.php');
    $query = 'SELECT * FROM item as i, item_cate as c, item_country as n WHERE i.item_cate=c.cate_id AND i.item_country=n.country_id ORDER BY item_id DESC, item_status ASC';
    $result = mysql_query($query, $con);
    $count = 1;
    while ($data = mysql_fetch_assoc($result)) {
        echo '
            <tr>
                <td class="list_text">' . $count . '</td>
                <td class="list_text">' . $data['item_name'] . '</td>
                <td class="list_text">' . $data['cate_name'] . '</td>
                <td class="list_text">' . $data['country_name'] . '</td>
                <td class="list_text">' . $data['item_quantity'] . '</td>
                <td class="list_text">';
        switch ($data['item_status']) {
            case 1:
                echo '<strong style="color:green;">Đang bán</strong>';
                break;
            case 2:
                echo '<strong style="color:orange;">Hết hàng</strong>';
                break;
            case 3:
                echo '<strong style="color:blue;">Liên hệ</strong>';
                break;
            case 4:
                echo '<strong style="color:red;">Ngưng bán</strong>';
                break;
        }
        echo '</td>
                <td class="list_text">' . date("d/m/Y", $data['item_date_in']) .
            '</td>
                <td class="list_text"><a href="item_edit.php?id=' . $data['item_id'] .
            '" class="btnEdit"></a><a href="item_del.php?id=' . $data['item_id'] .
            '" class="btnDel" onclick="return user_confirm(\'Bạn có muốn xóa hàng hóa này không?\');"></a></td>
            </tr>';
        $count++;
    }
?>
</table>
<?php
} elseif ($name == "Cate List") {
    /**
     * ***********************************************************************
     * CATE LIST
     * ***********************************************************************/
?>

<table class="data_list">
    <tr>
        <th class="list_text list_title" style="width: 50px;">STT</th>
        <th class="list_text list_title" >Loại hàng</th>
        <th class="list_text list_title"style="width: 50px;">Thứ tự</th>
        <th class="list_text list_title"style="width: 80px;">Quản lý</th>
    </tr>
    <?php
    require ('../lib/db_connect.php');
    //Lọc kết quả và sắp xếp giảm dần theo cate_id và tăng dần theo cate_order
    $query = "SELECT * FROM item_cate ORDER BY cate_order ASC, cate_id DESC";
    $result = mysql_query($query, $con);
    if (mysql_num_rows($result) == 0) {
        echo '<td colspan="4" style="text-align: center;color:red; padding: 10px;"><b>Chưa có Danh mục nào được thêm vào</b></td>';
    } else {
        $count = 1;
        while ($data = mysql_fetch_assoc(($result))) {
            //Kiểm tra xem parent có child hay không
            if ($data['cate_child'] == 0) {
                echo '
                                <tr>
                                    <td class="list_text">' . $count . '</td>
                                    <td class="list_text" style="text-align:left;">' .
                    $data['cate_name'] . '</td>
                                    <td class="list_text">' . $data['cate_order'] .
                    '</td>
                                    <td class="list_text"><a href="cate_edit.php?id=' .
                    $data['cate_id'] . '" class="btnEdit"></a><a href="cate_del.php?id=' . $data['cate_id'] .
                    '" class="btnDel" onclick="return user_confirm(\'Bạn có muốn xóa chuyên mục này không?\');"></a></td>
                                </tr>';
                $count++;
                //Khởi tạo query lọc kết quả các child cate
                $query_child = 'SELECT * FROM item_cate WHERE cate_child=' . $data['cate_id'] .
                    ' ORDER BY cate_order ASC, cate_id DESC';
                $result_child = mysql_query($query_child, $con);
                if (mysql_num_rows($result_child) != 0) {
                    $sub = $count - 1;
                    $count_child = 1;
                    while ($data_child = mysql_fetch_assoc($result_child)) {
                        echo '
                                <tr>
                                    <td class="list_text" style="background: #EDEDED;">' .
                            $sub . '.' . $count_child . '</td>
                                    <td class="list_text" style="background: #EDEDED;text-align:left;"> -- ' .
                            $data_child['cate_name'] . '</td>
                                    <td class="list_text" style="background: #EDEDED;">' .
                            $data_child['cate_order'] . '</td>
                                    <td class="list_text" style="background: #EDEDED;"><a href="cate_edit.php?id=' .
                            $data_child['cate_id'] . '" class="btnEdit"></a><a href="cate_del.php?id=' . $data_child['cate_id'] .
                            '" class="btnDel" onclick="return user_confirm(\'Bạn có muốn xóa chuyên mục này không?\');"></a></td>
                                </tr>';
                        $count_child++;
                    }
                }
            }
        }
    }
?>
</table>   

<?php
} elseif ($name == "Country List") {
    /**
     * ***********************************************************************
     * COUNTRY LIST
     * ***********************************************************************/
?>

<table class="data_list">
    <tr>
        <td class="list_text list_title" style="width: 50px;">STT</td>
        <td class="list_text list_title" >Quốc gia</td>
        <td class="list_text list_title"style="width: 80px;">Quản lý</td>
    </tr>
    <?php
    require ('../lib/db_connect.php');
    $query = "SELECT * FROM item_country";
    $result = mysql_query($query, $con);
    if (mysql_num_rows($result) == 0) {
        echo '<td colspan="3" style="text-align: center;color:red; padding: 10px;"><b>Chưa có Quốc gia nào được thêm vào</b></td>';
    } else {
        $count = 1;
        while ($data = mysql_fetch_assoc(($result))) {
            echo '
                    <tr>
                        <td class="list_text">' . $count . '</td>
                        <td class="list_text">' . $data['country_name'] . '</td>
                        <td class="list_text"><a href="country_edit.php?id=' . $data['country_id'] .
                '" class="btnEdit"></a><a href="country_del.php?id=' . $data['country_id'] .
                '" class="btnDel" onclick="return user_confirm(\'Bạn có chắc muốn xóa quốc gia này khỏi danh sách?\');"></a></td>
                    </tr>';
            $count++;
        }
    }
?>
</table>

<?php
} else {
    echo '<p class="warning">Lỗi trang không tìm thấy!</p>';
}

?>